﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SistemaGO_Desktop.Visao.Telas.Relatorios;

namespace SistemaGO_Desktop.Visao.Telas
{
    public partial class frmRelatorioUsuarios : Form
    {
        private static frmRelatorioUsuarios instancia = null;
        relatorioUsuarios ru = new relatorioUsuarios();

        private frmRelatorioUsuarios()
        {
            InitializeComponent();
        }

        public static frmRelatorioUsuarios getInstance() {
            if (instancia == null) {
                instancia = new frmRelatorioUsuarios();
            } else {
                instancia.Focus();
            }
            return instancia;
        }

        private void frmRelatorioUsuarios_FormClosing(object sender, FormClosingEventArgs e)
        {
            instancia = null;
        }

        private void limparTela()
        {
            cbRelatorioUsuario.Text = "Selecione";
            tbRelatorioUsuario.Text = "";       
        } 

        private void btRelatorioFiltrarUsuario_Click(object sender, EventArgs e) {
            try
            {
                string cbUsuario = cbRelatorioUsuario.Text;
                string tbValor = tbRelatorioUsuario.Text;
                ru.SetDataSource(sistemaGODataSet3);
                if (!cbUsuario.Trim().Equals("Selecione"))
                {
                    if (cbUsuario.Trim().Equals("Login"))
                    {
                        tbusuarioBindingSource.Filter = "usu_login like '%" + tbValor + "%'";
                        ru.RecordSelectionFormula = "{tb_usuario.usu_login}='" + tbValor + "'";
                    }
                    if (cbUsuario.Trim().Equals("Nome"))
                    {
                        tbusuarioBindingSource.Filter = "usu_nome like '%" + tbValor + "%'";
                        ru.RecordSelectionFormula = "{tb_usuario.usu_nome}='" + tbValor + "'";
                    }
                    if (cbUsuario.Trim().Equals("Sobrenome"))
                    {
                        tbusuarioBindingSource.Filter = "usu_sobrenome like '%" + tbValor + "%'";
                        ru.RecordSelectionFormula = "{tb_usuario.usu_sobrenome}='" + tbValor + "'";
                    }                    
                }
                else
                {
                    tbusuarioBindingSource.RemoveFilter();                    
                    ru.RecordSelectionFormula = null;                                        
                }

                this.tb_usuarioTableAdapter.Fill(this.sistemaGODataSet3.tb_usuario);
                crystalReportViewer1.ReportSource = ru;
                this.btRelatorioUsuarioExportar.Enabled = true;
            }
            catch
            {
                MessageBox.Show("Usuário(s) não encontrado(s)", "Falha na Busca", MessageBoxButtons.OK, MessageBoxIcon.Information);
                limparTela();
                cbRelatorioUsuario.Focus();
            }
        }

        private void btRelatorioUsuarioExportar_Click(object sender, EventArgs e) {
            crystalReportViewer1.ExportReport();
        }       

        private void frmRelatorioUsuarios_Load(object sender, EventArgs e)
        {
            
        }
                
        private void cbRelatorioUsuario_SelectedValueChanged(object sender, EventArgs e)
        {          
            if (cbRelatorioUsuario.Text.Equals("Login"))
            {
                label2.Text = "Login:";
            }
            else if (cbRelatorioUsuario.Text.Equals("Nome"))
            {
                label2.Text = "Nome:";
            }
            else if (cbRelatorioUsuario.Text.Equals("Sobrenome"))
            {
                label2.Text = "Sobrenome:";
            }            
        }

        private void cbRelatorioUsuario_Leave(object sender, EventArgs e)
        {
            if (cbRelatorioUsuario.Text.Equals(""))
            {
                cbRelatorioUsuario.Text = "Selecione";
                label2.Text = "Valor:";
            }
        }
    }
}
